
$(function() {

	$("input[id^='mold_s']:visible").autocomplete(Test.mold);
	
	$("input[id^='machine_s']:visible").autocomplete(Test.machine);
	
	$("input[id^='worker_s']:visible").autocomplete(Test.emp);
	
	Tip.emp('负责人');
});

var Plan = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.machine = form.machine.value;
				body.worker = form.worker.value;
				
				body.mold = form.mold.value;
				body.type = form.type.value;
				
				body.cavity = form.cavity.value;
				body.period = form.period.value;
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
				body.material = form.material.value;
				body.color = form.color.value;
				body.amount = form.amount.value;
				
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;								
				body.content = form.content.value;
				
				$(this).dialog("close");
				
				$.post('zs.php?do=Plan.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	view: function(id) {
		var url = 'zs.php?do=Plan.view&id=' + id;
		
		Common.dialog_view_(url, '查看信息', '650x400');
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'zs.php?do=Plan.edit&id=' + id + p2;
		var c2 = 'zs.php?do=Plan.view&id=' + id;
		var c3 = 'zs.php?do=Plan.copy&id=' + id;

		var cs = 'zs.php?do=Plan.status&id=' + id + p2;
			
		var c5 = 'zs.php?do=Plan.reason&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'Plan.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		
		var ab = ['打印信息', 'url', 'jswin', '1024x768'];	
			
		var a5 = ['暂停注塑', 'url', 'Plan.dialog_stop', '650x400'];
		var a6 = ['取消注塑', 'url', 'Plan.dialog_cancel', '650x400'];
		
		// 等待注塑
		if (status == 1) {
			var b1 = ['开机注塑', 'url', 'Plan.list_start'];
			
			var s = cs + "&status=2, " + c1 + ", " + c2 + ", " + c5 + ", " + c5;

			return this.popup_(e, [b1, a1, a2, a5, a6], s);
		}

		// 正在注塑
		if (status == 2) {
			var d1 = 'zs.php?do=Plan.finish&id=' + id;		
			var b1 = ['完成注塑', 'url', 'Plan.dialog_finish', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		// 完成注塑
		if (status == 3) {
			var s = c2;

			return this.popup_(e, [a2], s);
		}
		
		// 暂停注塑
		if (status == 5) {		
			var d1 = 'zs.php?do=Plan.status_continue&id=' + id + p2;
			var b1 = ['继续安排', 'url', 'Plan.list_continue'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		// 取消注塑
		if (status == 6) {
			var d1 = 'zs.php?do=Plan.status_restart&id=' + id + p2;
			var b1 = ['重启安排', 'url', 'Plan.list_restart'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'zs.php?do=Plan.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Plan.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('zs.php?do=Plan.audit&item=40', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'zs.php?do=Plan.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.machine = form.machine.value;
						body.worker = form.worker.value;
						
						body.mold = form.mold.value;
						body.type = form.type.value;
						
						body.cavity = form.cavity.value;
						body.period = form.period.value;
						body.weight1 = form.weight1.value;
						body.weight2 = form.weight2.value;
						body.material = form.material.value;
						body.color = form.color.value;
						body.amount = form.amount.value;
						
						body.t1 = form.date1.value + " " + form.time1.value;
						body.t2 = form.date2.value + " " + form.time2.value;
						body.content = form.content.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
												
						$(this).dialog("close");

						var url = 'zs.php?do=Plan.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'zs.php?do=Plan.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'zs.php?do=Plan.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	list_start: function (url, view) {
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否开机注塑？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.tt = form.tt.value;
						body.cost = form.cost.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'zs.php?do=Plan.finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_restart: function (url, view) {
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重启派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('zs.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要继续派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	}
};
